package cc.atliberty.oa.controller;

import cc.atliberty.oa.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/checkRegister")
public class CheckRegisterServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        int checked = userService.checkUser(username);
        if (checked != 1){
            response.getWriter().print("{\"msg\" : \"<font color='green'>该用户名可以使用</font>\"}\n");
        } else {
            String msg = "{\"msg\" : \"<font color='red'>该用户名已被使用</font>\"}";
            response.getWriter().print(msg);
        }
    }
}
